#include "config.h"
#include <QDebug>
#include <QTextCodec>
#include <QSettings>
#include <QStringList>

int Config::type = 99;
QString Config::serIp = "127.0.0.1";
long Config::serPort = 8001;
QString Config::ip = "127.0.0.1";
long Config::port = 8001;
QString Config::gameSaveDir = "";
WId Config::hwnd = NULL;
//int i = 1;
Config::Config()
{//qApp->applicationDirPath()

}
Config::~Config()
{
    qDebug()<<"~Config";
}
void Config::initConfig(QString dir){
   QSettings *ConfigIni = new QSettings( dir+"/config.ini",QSettings::IniFormat,0);
    ConfigIni->setIniCodec(QTextCodec::codecForName("UTF-8"));
    QStringList groups = ConfigIni->childGroups();

    if(groups.size()>0){
       ConfigIni->beginGroup(groups.at(0));
          Config::type = ConfigIni->value("type").toInt();
          Config::serIp = ConfigIni->value("serIp").toString();
          Config::serPort = ConfigIni->value("serPort").toString().toLong();
          Config::ip = ConfigIni->value("ip").toString();
          Config::port = ConfigIni->value("port").toString().toLong();
          Config::gameSaveDir = ConfigIni->value("gameSaveDir").toString();
       ConfigIni->endGroup();
    }
    //qDebug()<<Config::type << Config::serIp << Config::serPort << Config::ip << Config::port << Config::gameSaveDir  ;
    delete ConfigIni;
}

void Config::saveConfig(QString dir){
    QSettings *ConfigIni = new QSettings( dir+"/config.ini",QSettings::IniFormat,0);
    ConfigIni->setIniCodec(QTextCodec::codecForName("UTF-8"));
    QString temp = "config";
    QStringList groups = ConfigIni->childGroups();
    if(groups.size()>0){
        temp = groups.at(0);
    }
    ConfigIni->beginGroup(temp);
       ConfigIni->setValue("type",Config::type);
       ConfigIni->setValue("serIp",Config::serIp);
       ConfigIni->setValue("serPort",QString::number(Config::serPort));
       ConfigIni->setValue("ip",Config::ip);
       ConfigIni->setValue("port",QString::number(Config::port));
       ConfigIni->setValue("gameSaveDir",Config::gameSaveDir);
    ConfigIni->endGroup();
    delete ConfigIni;
}
